:- use_module(iop_sib_access).
:- dynamic msgtriple/3.
:- dynamic changed/0.

cached(triple('sib:any', 'http://sofia.gotdns.com/ontologies/SemanticConnections.owl#connectedTo', 'http://www.sofia-project.eu/ontologies/core/2010/01/19/sofia.owl#LumEnActive_A1003')).

cached(triple('sib:any', 'http://www.sofia-project.eu/ontologies/core/2010/01/19/sofia.owl#launchesEvent', 'sib:any')).
cached(triple('sib:any', 'rdf:type', 'http://www.sofia-project.eu/ontologies/core/2010/01/19/sofia.owl#Event')).
cached(triple('sib:any', 'http://sofia.gotdns.com/ontologies/SemanticConnections_UnBo.owl#hasEventType', 'sib:any')).


%url
ns_core('http://www.sofia-project.eu/ontologies/core/2010/01/19/sofia.owl#').
ns_semint('http://sofia.gotdns.com/ontologies/SemanticInteraction.owl#').
ns_sc('http://sofia.gotdns.com/ontologies/SemanticConnections.owl#').
ns_sc_add('http://sofia.gotdns.com/ontologies/SemanticConnections_UnBo.owl#').

%value
kp_name('LumEnActive_A1003').
id_type('EPC_RFID').
id('Identification_01').
id_value('01').

event_name('ui_mode(resize)').
event_name('ui_mode(scrible)').
event_name('ui_mode(mouse)').
event_name('ui_mode(record)').
event_name('ui_mode(play)').


%MediaPlayer constructor
lea_uri(X):- kp_name(Y), ns_core(Z), concat(Z, Y, X).

%Identification constructor
id_uri(X):- id(Y), ns_core(Z), concat(Z, Y, X).
id_type_uri(X):- id_type(Y), ns_semint(Z), concat(Z, Y, X).

%Event constructor
%event_uri(+NameIn, -UriOut)
event_uri(N,X):- ns_sc(Z), concat(Z, N, X).

%New Part 1
event_insert(Mpe):- findall(X, event_name(X), L), length(L, Num), event_function(L, Mpe, Num).

event_function([X|Ls], Mpe, Num):- Num >0, event_uri(X, Out), insert_sib(triple(Out, 'rdf:type', Mpe)), Z is Num-1,  event_function(Ls, Mpe, Z).
event_function(_,_,0).

%New Part 2

supportedEvent_insert(Lea,Set):- findall(X, event_name(X), L), length(L, Num), supportedEvent_function(L, Lea, Set, Num).

supportedEvent_function([X|Ls], Lea, Set, Num):- Num >0, event_uri(X, Out), insert_sib(triple(Lea, Set, Out)), Z is Num-1,  supportedEvent_function(Ls, Lea, Set, Z).  
supportedEvent_function(_,_,_,0).

%MediaPlayer
mediaPlayer(X):- ns_core(Y), concat(Y,'MediaPlayer', X).
identification(X):- ns_core(Y), concat(Y,'Identification', X).
idType(X):- ns_semint(Y), concat(Y,'IDType', X).

%Event
event(X):- ns_core(Y), concat(Y,'Event', X).
mediaPlayerEvent(X):- ns_core(Y), concat(Y,'MediaPlayerEvent', X).

%MediaPlayer smartification
hasIdentification(X):- ns_core(Y), concat(Y,'hasIdentification', X).
idValue(X):- ns_semint(Y), concat(Y,'idValue', X).
ofIDType(X):- ns_semint(Y), concat(Y,'ofIDType', X).

%MediaPlayer event smartification
supportEventType(X):- ns_sc_add(Y), concat(Y,'supportEventType', X).

%Connection
connectedTo(X):- ns_sc(Y), concat(Y,'connectedTo', X).

%Event Handling
event(X):- ns_core(Y), concat(Y,'Event',X).
launchesEvent(X):- ns_core(Y), concat(Y,'launchesEvent',X).
hasEventType(X):- ns_sc_add(Y), concat(Y,'hasEventType',X).
hasFriendlyName(X) :- ns_core(Y), concat(Y,'hasFriendlyName',X).

%costruct_ns_core(X,Y):-ns_core(NS),

test:-
	lea_uri(Lea),
    mediaPlayer(MediaPlayer),
    id_type_uri(Iduritype),
    idType(Idtype),
    id_uri(Iduri),
    identification(Identification),
    hasIdentification(Hasidentification),
    ofIDType(Ofidtype),
    idValue(Idvalue),
    mediaPlayerEvent(Mediaplayerevent),
    event(Event),
    supportEventType(Supporteventtype),
    id_value(ID),
	hasFriendlyName(FriendlyName),
	write(Lea),nl,
	write(MediaPlayer),nl,
	write(Iduritype),nl,
	write(Idtype),nl,
	write(Iduri),nl,
	write(Identification),nl,
	write(Hasidentification),nl,
	write(Ofidtype),nl,
	write(Idvalue),nl,
	write(Mediaplayerevent),nl,
	write(Event),nl,
	write(Supporteventtype),nl,
	write(ID),nl,
	write(FriendlyName),nl.
	

init :-
	%join_sib('192.168.5.211','X',10010),
    %join_sib('192.168.1.105','X',10010),
	join_sib('192.168.1.107','X',10010),
    %join_sib('172.16.123.128','X',10010),

    kp_name(Name),
	write(Name),nl,
    %write(P0),nl,
    %write(P1),nl,
    %write(P2),nl,
    %write(P3),nl,

    %costrizione delle triple per il lea 
    lea_uri(Lea),
    mediaPlayer(MediaPlayer),
    id_type_uri(Iduritype),
    idType(Idtype),
    id_uri(Iduri),
    identification(Identification),
    hasIdentification(Hasidentification),
    ofIDType(Ofidtype),
    idValue(Idvalue),
    %id_value(Idvalue),
    mediaPlayerEvent(Mediaplayerevent),
    event(Event),
    supportEventType(Supporteventtype),
    id_value(ID),
	hasFriendlyName(FriendlyName),

    %sottoscrivo le triple di interesse
    %assertz(cached(triple('sib:any', 'http://sofia.gotdns.com/ontologies/SemanticConnections.owl#connectedTo',Lea))),


    %smartificazione del Lea
    insert_sib(triple(Lea,'rdf:type',MediaPlayer)),
	insert_sib(triple(Lea,FriendlyName,'LEA01')),
    insert_sib(triple(Iduri,'rdf:type',Identification)),
    insert_sib(triple(Lea,Hasidentification,Iduri)),
	insert_sib(triple(Iduritype,'rdf:type',Idtype)),
    insert_sib(triple(Iduri,Ofidtype,Iduritype)),
    insert_sib(triple(Iduri,Idvalue,ID)),
	
	event_insert(Mediaplayerevent), 
	supportedEvent_insert(Lea, Supporteventtype), 

	subscribe_sib,
	
	true.

event_list(List):-
    findall(Typeofevent, Typeofevent, List),
    lea_uri(Uri),
    launchesEvent(Le),
    hasEventType(Het),
    event(Event),
    connectedTo(Ct),
    rdf(Smartobject, Le, Eventuuid),
    rdf(Eventuuid, 'rdf:type', Event),
    rdf(Eventuuid, Het, Typeofevent),
    event_name(Typeofevent),
    rdf(Smartobject, Ct, Uri).

write_all([]).
write_all([X|Ls]):- write(X), nl, write_all(Ls).

%message_available:- query_sib, event_list(L), write_all(L).
	
process :-
%	process_subscription,
	true.


cleanup :-
	(
		unsubscribe_sib,
		process_subscription,
		process_subscription,
		fail
	;
% only for UMVS: remove location and status for GIS
%		remove_location, remove_status, remove_all,
		leave_sib
	).


%
% It checks connections from local cache, make/break a connection by
% insert/remove in the SIB.
%

% list all connected devices (with stripped namespace)
%
% prepend namespace, then query all connected devices from local RDF store
% and strip their namespace:

peers(Name,ListOfPeers) :-
strip_namespace(NameSpacedName,Name),
findall(Peer, (rdf(NameSpacedName,
'http://sofia.gotdns.com/ontologies/SemanticConnections.owl#connectedTo',
NameSpacedPeer),
strip_namespace(NameSpacedPeer, Peer)), ListOfPeers).

% remove semantic connections
%
% temporary 'hack': self connect means disconnect all outgoing and incoming

connect('FamilyBondingDevice2','FamilyBondingDevice2') :-
connect('surroundSoundSystem','surroundSoundSystem'),!.

connect(Name,Name) :-
!,
strip_namespace(NameSpacedName1,Name),
(
rdf(NameSpacedName1,
'http://sofia.gotdns.com/ontologies/SemanticConnections.owl#connectedTo',
NameSpacedName2),
remove_sib([
triple(NameSpacedName1,
'http://sofia.gotdns.com/ontologies/SemanticConnections.owl#connectedTo',
NameSpacedName2),
triple(NameSpacedName2,
'http://sofia.gotdns.com/ontologies/SemanticConnections.owl#connectedTo',
NameSpacedName1)]),
fail
;
query_sib
).

% connect two devices
connect(Name1,Name2) :-
% prepend namespace
strip_namespace(NameSpacedName1,Name1),
strip_namespace(NameSpacedName2,Name2),
insert_sib(triple(NameSpacedName1,'http://sofia.gotdns.com/ontologies/SemanticConnections.owl#connectedTo',NameSpacedName2)),
insert_sib(triple(NameSpacedName2,'http://sofia.gotdns.com/ontologies/SemanticConnections.owl#connectedTo',NameSpacedName1)),
query_sib.

disconnect(Name1,Name2) :-
strip_namespace(NameSpacedName1,Name1),
strip_namespace(NameSpacedName2,Name2),
remove_sib([
triple(NameSpacedName1,'http://sofia.gotdns.com/ontologies/SemanticConnections.owl#connectedTo',NameSpacedName2),
triple(NameSpacedName2,'http://sofia.gotdns.com/ontologies/SemanticConnections.owl#connectedTo',NameSpacedName1)
]),
query_sib.

strip_namespace(NameIncludingNamespace,NameNoNamespace) :-
atom_concat('http://sofia.gotdns.com/ontologies/SemanticConnections.owl#', NameNoNamespace, NameIncludingNamespace),!.
strip_namespace(X,X).


%for debugging...
show_connections :-
rdf(N1,'http://sofia.gotdns.com/ontologies/SemanticConnections.owl#connectedTo',N2),
strip_namespace(N1,NNN1),
strip_namespace(N2,NNN2),
write(NNN1), write(' connected to '), writeln(NNN2),
fail.
show_connections :-
!.

%some obsolete stuff...
connected(Name,Date) :-
rdf(X,
'http://www.w3.org/1999/02/22-rdf-syntax-ns#type',
'http://sofia.gotdns.com/ontologies/SemanticConnections.owl#ConnectEvent'),
rdf(X,
'http://sofia.gotdns.com/ontologies/SemanticConnections.owl#generatedBy',
NameSpacedName),
rdf(X,
'http://sofia.gotdns.com/ontologies/SemanticConnections.owl#inXSDDateTime',
Date),
strip_namespace(NameSpacedName, Name).

disconnected(Name,Date) :-
rdf(X,
'http://www.w3.org/1999/02/22-rdf-syntax-ns#type',
'http://sofia.gotdns.com/ontologies/SemanticConnections.owl#DisonnectEvent'),
rdf(X,
'http://sofia.gotdns.com/ontologies/SemanticConnections.owl#generatedBy',
NameSpacedName),
rdf(X,
'http://sofia.gotdns.com/ontologies/SemanticConnections.owl#inXSDDateTime',
Date),
strip_namespace(NameSpacedName, Name).






message_available :-
	query_sib,
    %connectedTo(Con),
    rdf(SmartObj ,_,_),
    rdf(SmartObj,_,Ev),
    rdf(Ev,'rdf:type','http://www.sofia-project.eu/ontologies/core/2010/01/19/sofia.owl#Event'),!,
    rdf(Ev,'http://sofia.gotdns.com/ontologies/SemanticConnections_UnBo.owl#hasEventType',LeaEvent),!,
    leaEventBehaviour(LeaEvent),
    remove_sib(triple(SmartObj,_,Ev)),
    remove_sib(triple(Ev,'rdf:type','http://www.sofia-project.eu/ontologies/core/2010/01/19/sofia.owl#Event')),
    remove_sib(triple(Ev,'http://sofia.gotdns.com/ontologies/SemanticConnections_UnBo.owl#hasEventType',LeaEvent)).
    %write(SmartObj),nl,
    %write(LeaEvent),nl

leaEventBehaviour(Ev):-
    ns_core(X),
    atom_concat(X,'ui_mode(resize)',Ev),!,
    write(Ev),nl.

leaEventBehaviour(Ev):-
    ns_core(X),
atom_concat(X,'ui_mode(scrible)',Ev),!,
write(Ev),nl.

leaEventBehaviour(Ev):-
    ns_core(X),
atom_concat(X,'ui_mode(mouse)',Ev),!,
write(Ev),nl.

leaEventBehaviour(Ev):-
    ns_core(X),
atom_concat(X,'ui_mode(record)',Ev),!,
write(Ev),nl.

leaEventBehaviour(Ev):-
    ns_core(X),
atom_concat(X,'ui_mode(play)',Ev),!,
write(Ev),nl.








